﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using DevExpress.XtraGrid.Views.Grid;
using Lamex.Interface;
using Lamex.Common;
using Lamex.Library.CommonClass;
using Lamex.Base;

namespace Lamex.mTube.UI
{
    //参考Lamex.mPPC.UI.frmPPC_HPL430
    public partial class frmTube_Overview : frmBaseChild
    {
        public frmTube_Overview()
        {
            InitializeComponent();
        }

        private void frmTube_Overview_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            TuebDataRefresh();
        }

        private void frmTube_Overview_Resize(object sender, EventArgs e)
        {
            splitContainerControl2.SplitterPosition = this.Width / 5;
            splitContainerControl3.SplitterPosition = this.Width / 5;
            splitContainerControl4.SplitterPosition = this.Width / 5;
            splitContainerControl5.SplitterPosition = this.Width / 5;
        }

        #region 按钮操作

        private void sbtnPlanning_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Demo), sender as ToolStripMenuItem);
        }

        private void sbtnCutting_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Demo), sender as ToolStripMenuItem);
        }

        private void sbtnWelding_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Demo), sender as ToolStripMenuItem);
        }

        private void sbtnPachaging_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Packaging), sender as ToolStripMenuItem);
        }

        private void sbtnWarehousing_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Demo), sender as ToolStripMenuItem);
        }
        private void lblLayout_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Layout), sender as ToolStripMenuItem);
        }

        private void lblKanban_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmTube_Layout), sender as ToolStripMenuItem);
        }

        #endregion

        //刷新开料进度
        private void TuebDataRefresh()
        {
            DataTable dt = new DataTable();
            string sqlWhere = " and DE.Product_TS like '%" + DateTime.Now.ToString("yyyy-MM-dd") + "%'";
            string sql = @" select distinct de.Product_TS,de.Order_Number,DR.MODULE_NAME,left(de.Part_No,8)+'00' Part_No_Weld,de.Part_No,de.Drawing_No,sum(de.Part_QTY) Part_QTY FROM [LamexMESManager].[dbo].[Transfer_Part_Detailed] DE
 JOIN [LamexMESManager].[dbo].[IE_Part_Routing] RO on RO.Part_No=DE.Part_No
 JOIN [LamexMESManager].[dbo].[IE_Routing_Combination] CO ON RO.GROUP_CODE=CO.GROUP_CODE AND CO.ROUTING_CODE like '%P05%' 
 LEFT JOIN [LamexMESManager].[dbo].[NC_PN_AL_Detail_Drawing] DR ON DR.DRAWING_NO=DE.Drawing_No
 LEFT JOIN [LamexMESManager].[dbo].[NC_PN_AL_Manufacture_Convert_Record] CR ON CR.PART_NO=DE.Part_No AND CR.FOLLOW_NO=DE.Order_Number 
where 1=1 AND CR.PART_NO IS NULL AND CR.FOLLOW_NO IS NULL " + sqlWhere + "  group by  de.Product_TS,de.Order_Number,de.Part_No,de.Drawing_No,DR.MODULE_NAME ORDER BY DE.Order_Number";
            dt = BaseBuildSql.GetDataTableBySql(sql);
            gridControl1.DataSource = dt;
            gridControl2.DataSource = dt;
            gridControl3.DataSource = dt;
            gridControl4.DataSource = dt;
            gridControl5.DataSource = dt;

            setGridViewAutoFitColumns(gridView1);
            setGridViewAutoFitColumns(gridView2);
            setGridViewAutoFitColumns(gridView3);
            setGridViewAutoFitColumns(gridView4);
            setGridViewAutoFitColumns(gridView5);
        }

        private void setGridViewAutoFitColumns(GridView gv)
        {
            gv.BestFitColumns();
            gv.OptionsView.ColumnAutoWidth = false;
        }

        public override void DoView(IButtonInfo sender)
        {
            TuebDataRefresh();
            base.DoView(sender);
        }

    }
}